using System;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath ;
using EnvDTE;
using AopDotNet.CodeModelXMLTreeBuilder;
using System.Collections ;
using System.Text.RegularExpressions  ;
using System.Text;
using System.IO ;

using System.CodeDom.Compiler  ;
using System.CodeDom ;


namespace AopDotNet
{
	namespace AspectWeaver
	{
		/// <summary>
		/// 
		/// </summary>
		public class Helper
		{
			public Helper()
			{
				// 
				// TODO: Add constructor logic here
				//
			}
			public static Project curProj;
			static public string GetFunctionBody(CodeFunction cf)
			{
				TextPoint strtPoint=cf.GetStartPoint(vsCMPart.vsCMPartBody);
				TextPoint endPoint=cf.GetEndPoint(vsCMPart.vsCMPartBody);
				return strtPoint.CreateEditPoint().GetText(endPoint);
			}
			/*static public void SaveTheElementFile(CodeElement element)
			{
				string filePath=element.ProjectItem.get_FileNames (0);
				string fileName=Helper.GetFileNameFromFullPath (filePath);
				ProjectItem item=curProj.ProjectItems.Item(fileName);
				if (item!=null)
				
				item.Document.Save("");
			
			}*/
			static public CodeElement GetCodeElementFromCodeModel(XmlNode codeModelNode,CodeModelBuilder CodeModelTree)
			{
				try
				{
					CodeElement res=(CodeElement) CodeModelTree .codeElements [Int32.Parse (codeModelNode.Attributes["id"].Value)];
					return res;
				}
				catch(Exception ex)
				{
					return null;
				}

			}
			static public CodeElement GetAspectElement(string elementName,string elementType,string aspectName,CodeModelBuilder codeTree)
			{
				return codeTree.SearchForElement("//aspect[@name='"+aspectName+"']/descendant::"+elementType+"[@name='"+elementName+"']");
			}
			static public void OpenItemFile(CodeElement element)
			{
				string fileName=element.ProjectItem.get_FileNames (0);
				try
				{
					element.DTE.OpenFile (Constants.vsViewKindPrimary ,fileName);
				}
				catch(Exception e)
				{
				}
			}
			//static public string GetStatementInFunction(CodeFunction cf,EditPoint start,EditPoint end)
			//{
			/*REM*/
			//	EditPoint strtPoint=Helper.GetEditPointBeforeStatementInFunction (ref cf,startIndex  );
					
			//	EditPoint endPoint=Helper.GetEditPointAfterStatementInFunction (ref cf,startIndex ,length );
			//	return strtPoint.GetText (endPoint);
					
			/*EditPoint startPt=cf.StartPoint.CreateEditPoint ();
			startPt.MoveToAbsoluteOffset (startIndex);

			EditPoint endPt=cf.StartPoint.CreateEditPoint ();
			endPt.MoveToAbsoluteOffset (length);

			return startPt.GetText (endPt);*/


			//	return start.GetText (endPt);
			//}
			//static public EditPoint GetEditPointBeforeStatementInFunction (ref CodeFunction advisedMethod,int startIndex  )
			//{
			/*REM*/
			//	EditPoint startPoint=advisedMethod.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint ();
		
			//	startPoint.MoveToAbsoluteOffset (advisedMethod.GetStartPoint(vsCMPart.vsCMPartBody).AbsoluteCharOffset +startIndex );
			//	return startPoint;
			//	}
			//	static public EditPoint GetEditPointAfterStatementInFunction (ref CodeFunction advisedMethod,int startIndex ,int length)
			//	{
			/*REM*/
			//		EditPoint startPoint=advisedMethod.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint ();
			//		startPoint.MoveToAbsoluteOffset (startPoint .AbsoluteCharOffset +startIndex+length );
			//startPoint .Insert ("/*aftMM*/");
			//		return startPoint;
			//	}
			static public string GetFileNameFromFullPath(string FullPath)
			{
				int index=FullPath.LastIndexOf (@"\");
				if (index!=-1)
				{
					return FullPath.Substring (index+1);
				}
				else
				{
					return FullPath;
				}

			}

		}
	}
}